package cn.wuqingna.legendgem.script

import cn.wuqingna.legendgem.api.LegendGemAPI
import org.bukkit.entity.Player
import kotlin.random.Random


object RandomScript: Script {
    override val key: String
        get() = "random"

    override fun eval(player: Player, args: String, cache: MutableMap<String, String>): String {
        try {
            val spilt = args.replace("{", "").replace("}", "").split(":")
            val type = spilt[1]


            when(type){
                "int" -> {
                    val value = spilt[2].split("-")
                    val min = value[0].toInt()
                    val max = value[1].toInt()
                    return ((min .. max).random()).toString()
                }
                "double" -> {
                    val value = spilt[2].split("-")
                    val min = value[0].toDouble()
                    val max = value[1].toDouble()

                    if (spilt.size == 4){
                        val df =  LegendGemAPI.decimalFormat
                        return df.format((min + Random.nextDouble() * (max - min)))
                    }
                    return (min + Random.nextDouble() * (max - min)).toString()
                }
            }
            return args
        } catch (e: Exception) {
            error("random 语句错误 $args")
        }
    }
}